﻿using ActionSystem.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ActionSystem.Core.AnimationLogic;
namespace ActionSystem.Core.ComboLogic
{
    [ActionSort(ActionSystemConstDefine.ActionSystemBoxSetUpActionSortValue)]
    [ActionConfig(typeof(SetBoxFromWeaponActionHandler))]
    public class SetBoxFromWeaponActionConifg : BaseStateActionConfig
    {
        public SetBoxFromWeaponActionConifg()
        {
            this.canInherited = true;
            this.canInheriting = true;
        }
    }
    /// <summary>
    /// 根据slot和配置的信息组装碰撞盒
    /// 这个配置信息可以写在配置文件中，运行时组装
    /// </summary>
    public class SetBoxFromWeaponActionHandler : BaseStateActionHandler,IHitBoxActionHandler
    {
        List<RangeBoxItem> attackBoxs;
        List<RangeBoxItem> bodyBoxs;
        BaseComboProcessAbility hitAbility;
        public void BoxSetUp(BaseStateAction action)
        {
            throw new System.Exception("还没设置");
            BaseState state = action.State;
            BaseStateMachine stateMachine = action.StateMachine;
            #region AttackBox
            {
                if (stateMachine.IsPaused)//暂停无法攻击
                {                    
                    if(attackBoxs.Count > 0)
                    {
                        foreach (var box in attackBoxs)
                        {
                            RangeBoxPool.PutBoxItem(box);
                        }
                        attackBoxs.Clear();
                    }                    
                }else
                {
                    if (!state.CurrentIsStayAttackRanges())
                    {
                        //清除旧的设置
                        foreach (var box in attackBoxs)
                        {
                            RangeBoxPool.PutBoxItem(box);
                        }
                        attackBoxs.Clear();
                        var attacks = state.GetCurrentAttackRanges();
                        if (attacks != null)//暂停了无法攻击
                        {
                            foreach (var a in attacks)
                            {
                                var rangebox = RangeBoxPool.GetBoxItem(stateMachine.GetUnityLayer(a.boxTypeID), a.value,stateMachine.GetUnityLayerMask(a.boxTypeID));                                
                                rangebox.AttachToSlotTransfrom(stateMachine.GetSlotTransform(a.slotID));
                                attackBoxs.Add(rangebox);
                            }
                        }
                    }
                }
                
            }
            #endregion
            #region BodyBox
            {
                if (!state.CurrentIsStayBodyRanges())
                {
                    //清除旧的设置
                    foreach (var box in bodyBoxs)
                    {
                        RangeBoxPool.PutBoxItem(box);
                    }
                    bodyBoxs.Clear();

                    var bodys = state.GetCurrentBodyRanges();
                    if (bodys != null)
                    {
                        foreach (var a in bodys)
                        {
                            var rangebox = RangeBoxPool.GetBoxItem(stateMachine.GetUnityLayer(a.boxTypeID),a.value);
                            rangebox.AttachToSlotTransfrom(stateMachine.GetSlotTransform(a.slotID));
                            bodyBoxs.Add(rangebox);
                        }
                    }
                }
            }
            #endregion
        }
        public void HitDetect(BaseStateAction action)
        {
            BaseState state = action.State;
            BaseStateMachine stateMachine = action.StateMachine;
            #region AttackBox
            {
                if (stateMachine.IsPaused)//暂停无法攻击
                {
                    return;
                }
                else
                {                                       
                    if (attackBoxs.Count>0)//暂停了无法攻击
                    {
                        foreach (var rangebox in attackBoxs)
                        {
                            int count = rangebox.HitDetect();
                            //Debug.Log(count);
                        }
                    }                    
                }
            }
            #endregion
        }
        public override void Enter(BaseStateAction action)
        {
            throw new System.Exception("还没设置");
            base.Enter(action);
            if(attackBoxs == null)
            {
                attackBoxs = new List<RangeBoxItem>();
            }
            if (bodyBoxs == null)
            {
                bodyBoxs = new List<RangeBoxItem>();
            }
            hitAbility = (action.StateMachine as IHitProcessAbility)?.GetHitProcessAbility();
        }
        public override void Exit(BaseStateAction action)
        {
            throw new System.Exception("还没设置");
            base.Exit(action);
            //清除旧的设置
            foreach (var box in attackBoxs)
            {
                RangeBoxPool.PutBoxItem(box);
            }
            foreach (var box in bodyBoxs)
            {
                RangeBoxPool.PutBoxItem(box);
            }
            attackBoxs.Clear();
            bodyBoxs.Clear();
            hitAbility = null;
        }

        
    }
}

